package com.fms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fms.entity.OutboundOrder;
import com.fms.entity.WarehouseHardware;
import com.fms.mapper.OutboundOrderMapper;
import com.fms.mapper.WarehouseHardwareMapper;
import com.fms.service.WarehouseHardwareService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class WarehouseHardwareServiceImpl extends ServiceImpl<WarehouseHardwareMapper, WarehouseHardware> implements WarehouseHardwareService {

    private final WarehouseHardwareMapper warehouseHardwareMapper;

    private final OutboundOrderMapper outboundOrderMapper;

    @Override
    public List<WarehouseHardware> getAllHardware() {
        LambdaQueryWrapper<WarehouseHardware> queryWrapper = new LambdaQueryWrapper<WarehouseHardware>();
        List<WarehouseHardware> warehouseHardwares = warehouseHardwareMapper.selectList(queryWrapper);
        return warehouseHardwares;
    }

    @Override
    public List<WarehouseHardware> getOutboundHardwareByOrderId(Long orderId) {
        LambdaQueryWrapper<OutboundOrder> outWrapper = new LambdaQueryWrapper<>();
        outWrapper.eq(OutboundOrder::getOrderId, orderId);
        List<OutboundOrder> outboundOrders = outboundOrderMapper.selectList(outWrapper);
        List<WarehouseHardware> warehouseHardware = new ArrayList<WarehouseHardware>();
        if (outboundOrders!=null) {
            for (OutboundOrder outboundOrder : outboundOrders){
                if("五金".equals(outboundOrder.getMaterialType())){
                    WarehouseHardware hardware = warehouseHardwareMapper.selectById(outboundOrder.getMaterialId());
                    hardware.setHardwareAddAmount(outboundOrder.getAddHardwareAmount());
                    warehouseHardware.add(hardware);
                }
            }
        }
        return warehouseHardware;
    }
}
